
/**
*tagfolder
*Licensed under LGPL
*http://code.google.com/p/tagfolder
*chuanshenglu@gmail.com
*xrjieyee@gmail.com
*
*/

#include "TFServerBuffer.h"
#include <fstream>
//#include <ostream>

//=======================================================================
//TFStringBuffer
//Currently , Full-load/Full-write-back strategy adopted!

//Load data to buffer,maybe some 
/*TFStringBuffer::TFStringBuffer ( std::string& dbfilename )
        :OpCount( 0 ),IdCount( 0 )
{
	if ( dbfilename == "" )
	{
		dbfilename.assign( "DefaultDB.txt" );
	}
	Buffer.clear();
	dbfile.assign( dbfilename );
	std::ifstream dat;
	dat.open( dbfile.c_str(), std::ifstream::binary | std::ifstream::in );
	std::string str;
	long index = 0;
	while( dat.good() )
	{
		str.clear();
		dat >> index;
		dat >> str;
                if( IdCount < index )
                {
                        IdCount = index;
                }
		Buffer.insert( std::pair<long, std::string>( index++,str ) );
	}
	dat.close();
}

TFStringBuffer::~TFStringBuffer ( )
{
        WriteBack();
}

//Write all data back to file
bool TFStringBuffer::WriteBack ( )
{
	std::ofstream dat( dbfile.c_str(), 
			  std::ofstream::binary | std::ofstream::out );
	for ( std::map< long,std::string>::iterator itr = Buffer.begin();
	      itr != Buffer.end();
	      itr++ )
	{
		std::pair<long,std::string> entry = *itr;
		dat <<entry.first<<entry.second <<std::endl;
	}
	dat.close();
        return true;
}

void TFStringBuffer::ShrinkBuffer ( )
{
}

long TFStringBuffer::Size ( )
{
	return Buffer.size();
}

std::string TFStringBuffer::GetById ( long id )
{
	std::string ret;
	std::map< long,std::string>::iterator itr = Buffer.find(id);
	if( itr != Buffer.end() )
	{
		ret = itr->second;
	}
	return ret;
}

std::string TFStringBuffer::GetByIndex ( long index )
{
	return GetById( index );
}

long TFStringBuffer::GetStringId( std::string& str )
{
        for( std::map<long, std::string>::iterator itr = Buffer.begin();
             itr != Buffer.end();
             itr++ )
        {
                if(itr->second == str)
                {
                        return itr->first;
                }
        }
        return (-1);
}

bool TFStringBuffer::Add ( std::string& str )
{
        Buffer.insert ( std::pair<long, std::string>( GetNextId(), str ) );
        return true;
}

bool TFStringBuffer::RemoveById ( long id )
{
	std::map<long, std::string>::iterator itr = Buffer.find( id );
	if( itr != Buffer.end() )
	{
		Buffer.erase(itr);
                return true;
	}
        OpCount++;
	return false;
}

bool TFStringBuffer::RemoveByIndex ( long index )
{
	return RemoveById( index );
}
*/
//=========================================================================
//TFBufferManager
TFBufferManager* TFBufferManager::instance=NULL;

TFBufferManager::TFBufferManager()
{
	StringBuffers.clear();
        LinkBuffers.clear();
	LegalBufferNames.clear();
	LegalBufferNames.insert( "TagBuffer" );
	LegalBufferNames.insert( "FileBuffer" );
        LegalBufferNames.insert( "LinkBuffer" );
}

TFBufferManager::~TFBufferManager()
{
	for(std::map<std::string, TFStringBuffer*>::iterator itr = StringBuffers.begin();
	    itr != StringBuffers.end();
	    itr++ )
	{
		TFStringBuffer *buf = itr->second;
		if( buf != NULL )
			delete buf;
	}
        for(std::map<std::string, TFLinkBuffer*>::iterator itr = LinkBuffers.begin();
            itr != LinkBuffers.end();
            itr++ )
        {
                TFLinkBuffer *buf = itr->second;
                if(buf != NULL )
                        delete buf;
        }
}

TFBufferManager& TFBufferManager::GetInstance()
{
	if( instance == NULL )
        {
		instance = new TFBufferManager();
        }
	return ( *instance );
}

//TFStringBuffer& TFBufferManager::GetBuffer(const char *bufname)
//{
//	std::string buf( bufname );
//	return GetBuffer( buf );
//}


TFStringBuffer& TFBufferManager::GetStringBuffer(std::string &bufname)
{
        std::map<std::string, TFStringBuffer*>::iterator itr = StringBuffers.find( bufname );
        if(itr == StringBuffers.end() && 
           LegalBufferNames.find( bufname ) != LegalBufferNames.end() )
        {
                std::pair<std::string, TFStringBuffer*> v;
                v.first = bufname;
                v.second = new TFStringBuffer( bufname );
                StringBuffers.insert( v );
                return *( v.second );
        }
        else
        {
                return *( itr->second );
        }
}

TFLinkBuffer& TFBufferManager::GetLinkBuffer(std::string& bufname)
{
        std::map<std::string, TFLinkBuffer*>::iterator itr = LinkBuffers.find( bufname );
        if(itr == LinkBuffers.end() && 
           LegalBufferNames.find( bufname ) != LegalBufferNames.end() )
        {
                std::pair<std::string, TFLinkBuffer*> v;
                v.first = bufname;
                v.second = new TFLinkBuffer( bufname );
                LinkBuffers.insert( v );
                return *( v.second );
        }
        else
        {
                return *( itr->second );
        }
}

